package com.geostar.constructionland.analyse.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.mapper.AdjustVersionMapper;
import com.geostar.constructionland.common.entity.response.AdjustAgrVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author yelll
 * @since 2020-7-10 10:43
 **/
@Service
public class BaseAdjustVersionService  extends ServiceImpl<AdjustVersionMapper,AdjustAgrVersion> {
    @Autowired
    AdjustVersionMapper adjustVersionMapper;
    public void setAdjustVersion(AnalyseModel analyseModel) throws Exception{
        AdjustAgrVersion adjVersion = null;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("adjust_type",analyseModel.getFxlx());
        if(StringUtils.isBlank(analyseModel.getAdjustVersion())){
            queryWrapper.orderByDesc("adjust_version");
            List<AdjustAgrVersion> adjustAgrVersions =
                    adjustVersionMapper.selectList(queryWrapper);
            if(adjustAgrVersions.size() > 0){
                adjVersion = adjustAgrVersions.get(0);
                analyseModel.setAdjustVersion(adjVersion.getAdjustVersion());
            }
        }
        if(adjVersion==null){
            throw new Exception("没有相应的平差算法版本信息");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        if (id.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(id.split(","));
            adjustVersionMapper.deleteBatchIds(versionIds);
        } else {
            adjustVersionMapper.deleteById(id);
        }
    }

    public IPage<AdjustAgrVersion> getAdjustVersionForPage(int pageNo, int pageNum, String query_param_adjust_version_name) {
        IPage<AdjustAgrVersion> adjustAgrVersionIPage = new Page<>(pageNo, pageNum);
        QueryWrapper<AdjustAgrVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("modified_time");
        if(StringUtils.isNotBlank(query_param_adjust_version_name)) {
            queryWrapper.like("adjust_type",query_param_adjust_version_name);
        }
        return adjustVersionMapper.selectPage(adjustAgrVersionIPage, queryWrapper);
    }
}
